﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 进度条操作
    /// </summary>
    public class ProgressHelper
    {
        /// <summary>
        /// 
        /// </summary>
        private static FrmProgress frmProgress = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="type"></param>
        /// <param name="max"></param>
        /// <param name="step"></param>
        public static void Show(Form owner, zjgisProgressType type, int max, int step = 1)
        {
            if (frmProgress == null || frmProgress.IsDisposed)
                frmProgress = new FrmProgress();
            else
                frmProgress.Visible = false;
            frmProgress.ProgressType = type;
            frmProgress.Max = max;
            frmProgress.StepOne = step;
            frmProgress.Owner = owner;
            frmProgress.Start();
            Application.DoEvents();
        }
        
        /// <summary>
        /// 根据step的值递增
        /// </summary>
        public static void StepValue()
        {
            frmProgress.Invoke(new Action(delegate() { frmProgress.StepValue(); }));
        }
        
        /// <summary>
        /// 递增到指定的值
        /// </summary>
        /// <param name="value"></param>
        public static void StepTo(int value)
        {
            Action<int> action = frmProgress.StepTo;
            frmProgress.Invoke(action, value);
        }
        
        /// <summary>
        /// 根据指定的值value递增
        /// </summary>
        /// <param name="value"></param>
        public static void Increment(int value)
        {
            Action<int> action = frmProgress.Increment;
            frmProgress.Invoke(action, value);
        }
        
        /// <summary>
        /// 停止进度条
        /// </summary>
        public static void Stop()
        {
            frmProgress.Invoke(new Action(delegate() { frmProgress.Stop(); }));
            frmProgress = null;
        }

    }

    /// <summary>
    /// 进度条
    /// </summary>
    public class Progressor
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public Progressor()
        {

        }

        /// <summary>
        /// 设置当前的值
        /// </summary>
        /// <param name="current"></param>
        delegate void SetProgress(int current);

        /// <summary>
        /// 获取或设置进度条对象
        /// </summary>
        public dynamic ProgressBar { get; set; }

        /// <summary>
        /// 最大值
        /// </summary>
        private int _Max = 0;

        /// <summary>
        /// 设置进度条的最大值
        /// </summary>
        /// <param name="max"></param>
        public void SetMaxValue(int max)
        {
            if (ProgressBar == null) return;
            if (ProgressBar.InvokeRequired)
            {
                Action<int> d = SetMaxValue;
                ProgressBar.Invoke(d, new object[] { max });
            }
            else
            {
                _Max = max;
                ProgressBar.Maximum = max;
                ProgressBar.Minimum = 0;
                ProgressBar.Value = 0;
                ProgressBar.Visible = true;
                ProgressBar.TextVisible = true;
                Application.DoEvents();
            }
        }

        /// <summary>
        /// 设置当前进度条的值
        /// </summary>
        /// <param name="current"></param>
        public void SetCurrentValue(int current)
        {
            if (ProgressBar.InvokeRequired)
            {
                Action<int> d = SetCurrentValue;
                ProgressBar.Invoke(d, new object[] { current });
            }
            else
            {
                if (current <= ProgressBar.Maximum)
                {
                    ProgressBar.Value = current;
                    double dRate = current * 100.0 / ProgressBar.Maximum;
                    int iRate = (int)Math.Floor(dRate);
                    ProgressBar.TextVisible = true;
                    ProgressBar.Text = current.ToString() + "/" + ProgressBar.Maximum.ToString() + "(" + iRate.ToString() + "%)";
                }
                else
                {
                    ProgressBar.Value = (int)ProgressBar.Maximum;
                    ProgressBar.TextVisible = false;
                }
                if (ProgressBar is ProgressBarX)
                    ProgressBar.Update();
                if (ProgressBar.Maximum < 200)
                    Application.DoEvents();
                else
                    if (current % (ProgressBar.Maximum / 100) == 0) Application.DoEvents();
            }
        }

        /// <summary>
        /// 递增
        /// </summary>
        /// <param name="value"></param>
        public void Step(int value = 1)
        {
            int current = ProgressBar.Value + value;
            SetCurrentValue(current);
        }

        //public void Set

        /// <summary>
        /// 停止进度条
        /// </summary>
        public void Stop()
        {
            if (ProgressBar.InvokeRequired)
            {
                Action d = Stop;
                ProgressBar.Invoke(d);
            }
            else
            {
                ProgressBar.Visible = false;
                ProgressBar.Value = (int)ProgressBar.Maximum;
                ProgressBar.TextVisible = false;
            }
        }
    }


    /// <summary>
    /// 进度条类型
    /// </summary>
    public enum zjgisProgressType
    {
        /// <summary>
        /// 动画滚动进度条
        /// </summary>
        Marquee,
        /// <summary>
        /// 标准的进度条
        /// </summary>
        Standard,
        /// <summary>
        /// 圆形滚动进度条
        /// </summary>
        Circle
    }

}
